package com.qcby.pushservice.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.framework.common.enums.DeleteFlagEnum;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import com.qcby.framework.common.pojo.Result;
import com.qcby.pushservice.base.dto.BusinessClientDTO;
import com.qcby.pushservice.base.mapper.BusinessClientMapper;
import com.qcby.pushservice.base.po.BusinessClientPO;
import com.qcby.pushservice.base.service.IBusinessClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试服务impl
 *
 * @author cong.zhen
 * @date 2023/04/11
 */
@Service
@Slf4j
public class BusinessClientImpl implements IBusinessClientService {

    @Resource
    private BusinessClientMapper businessClientMapper;

    

    @Override
    public Result<PageResult<BusinessClientDTO>> demoPageQuery(BusinessClientDTO BusinessClientDTO, PageParam pageParam){

        /**构造查询条件**/
        LambdaQueryWrapper<BusinessClientPO> lqw = buildWrapper(BusinessClientDTO);
        Page<BusinessClientPO> BusinessClientPOPage = businessClientMapper.selectPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lqw);
        /**po转dto**/
        List<BusinessClientDTO> BusinessClientDTOS = BeanUtil.copyToList(BusinessClientPOPage.getRecords(),BusinessClientDTO.class);
        /**构造返回体**/
        PageResult pageResult = new PageResult();
        pageResult.setList(BusinessClientDTOS);
        pageResult.setTotal(BusinessClientPOPage.getTotal());
        return Result.getSuccessResult(pageResult);
    }

    @Override
    public BusinessClientDTO getDemoById(Long id) {
        LambdaQueryWrapper<BusinessClientPO> lqw = new LambdaQueryWrapper<BusinessClientPO>().eq(BusinessClientPO::getId,id).eq(BusinessClientPO::getDelFlag,DeleteFlagEnum.NO_DELETE.getCode());
        BusinessClientPO BusinessClientPO = businessClientMapper.selectOne(lqw);
        return BeanUtil.copyProperties(BusinessClientPO,BusinessClientDTO.class);
    }

    @Override
    public Boolean saveOrUpdate(BusinessClientDTO BusinessClientDTO) {
        BusinessClientPO BusinessClientPO = BeanUtil.copyProperties(BusinessClientDTO,BusinessClientPO.class);
        int temp;
        /**如果有id就是更新，没有id就是保存**/
        if(ObjectUtil.isNull(BusinessClientDTO.getId())){
            temp = businessClientMapper.insert(BusinessClientPO);
        }else {
            temp = businessClientMapper.updateById(BusinessClientPO);
        }
       return temp > 0;
    }

    @Override
    public Boolean removeById(Long id) {
        LambdaUpdateWrapper<BusinessClientPO> updateWrapper = new LambdaUpdateWrapper<BusinessClientPO>().eq(BusinessClientPO::getId,id).set(BusinessClientPO::getDelFlag, DeleteFlagEnum.DELETE.getCode());
        int update = businessClientMapper.update(null, updateWrapper);
        return update > 0;
    }

    public LambdaQueryWrapper<BusinessClientPO> buildWrapper(BusinessClientDTO BusinessClientDTO){
        LambdaQueryWrapper<BusinessClientPO> lqw = new LambdaQueryWrapper<BusinessClientPO>();
        /**此处写自己的分页查询参数**/
        lqw.eq(StrUtil.isNotBlank(BusinessClientDTO.getBusinessCode()),BusinessClientPO::getBusinessCode, BusinessClientDTO.getBusinessCode());
        lqw.eq(StrUtil.isNotBlank(BusinessClientDTO.getBusinessName()),BusinessClientPO::getBusinessName, BusinessClientDTO.getBusinessName());
        lqw.eq(BusinessClientPO::getDelFlag,DeleteFlagEnum.NO_DELETE.getCode());
        /**根据id去排序，不要根据创建时间**/
        lqw.orderByDesc(BusinessClientPO::getId);
        return lqw;
    }

}
